package fr.afcepf.atod18.ws_coordination;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.apache.cxf.jaxrs.client.JAXRSClientFactory;

import fr.afcepf.atod18.ws.currencies.CurrencyDTO;
import fr.afcepf.atod18.ws.currencies.IServiceCurrencies;
import fr.afcepf.atod18.ws.currencies.ServiceCurrenciesService;

@Path("/ServiceChangeWithFee")
@Produces("application/json")
public class ServiceChangeWithFee implements IServiceChangeWithFee {
    /* (non-Javadoc)
     * @see fr.afcepf.atod18.ws_coordination.IServiceChangeWithFee#getCurrenciesList()
     */
    @GET
    @Path("/getCurrenciesList")
    public CurrenciesDTO getCurrenciesList() {
        System.out.println("Enter getCurrenciesList of REST Service ServiceChangeWithFee");
        IServiceCurrencies proxyServiceCurrencies =  (new ServiceCurrenciesService() ).getServiceCurrenciesPort();
        CurrenciesDTO cs = new CurrenciesDTO();
        cs.setListCurrency( proxyServiceCurrencies.getAllCurrenciesEuroBased() );
        for ( CurrencyDTO currency:cs.getListCurrency()) {
            System.out.println(currency.getCountryName()+" "+currency.getCurrencyName());
        }
        return cs;
    }
    
    @GET
    @Path("/getChangeWithFee/{Amount}/{CurrencyIn}/{CurrencyOut}")
    public ChangeDTO getChangeWithFee(@PathParam("Amount") double paramAmount,
            @PathParam("CurrencyIn") String currencyIn,
            @PathParam("CurrencyOut") String currencyOut ) {
        

        System.out.println("ServiceChangeWithFee getChangeWithFee begin "+paramAmount+" "+currencyIn+" "+currencyOut);
        /*
        * create intance of proxy for SOAP WebService IServiceCurrencies
        */
        IServiceCurrencies proxyServiceCurrencies =  (new ServiceCurrenciesService() ).getServiceCurrenciesPort();
        // call SOAP WebService IServiceCurrencies getChangeRateForCurrency
        List<CurrencyDTO> lc = proxyServiceCurrencies.getChangeRateForCurrency(currencyOut);
        if (lc.size() < 1) {
            return new ChangeDTO( -1, -1 ); 
        }
        System.out.println("ServiceChangeWithFee Fee Country  "+lc.get(0).getCountryName()+" rate "+lc.get(0).getCountryName());
        // call SOAP WebService IServiceCurrencies getChange
        double retchange = proxyServiceCurrencies.getChange(paramAmount, currencyIn, currencyOut);
        System.out.println("ServiceChangeWithFee getChange "+retchange);
        /*
         *  call RESTful WebService IServiceChangeFee.getFeeForAmountAndCountry 
        */
        IServiceChangeFee scf = JAXRSClientFactory.create("http://localhost:8080/wrcurrencies2/services", IServiceChangeFee.class);
        double retfee = scf.getFeeForAmountAndCountry(retchange, lc.get(0).getCountryName());
        System.out.println("ServiceChangeWithFee getFeeForCountry "+retfee);
        return new ChangeDTO( retchange, retfee );
    }
}
